package com.shop.dao.impl;

import com.shop.dao.UserDao;
import com.shop.entity.User;
import com.shop.exception.DAOException;
import com.shop.transaction.TransactionManager;
import com.shop.util.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    public User userRex(String name, String pwd) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "select * from user where u_name=? and u_pwd=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,pwd);
            rs = ps.executeQuery();
            if(rs.next()) {
                user = new User();
                user.setU_id(rs.getInt("u_id"));
                user.setU_name(name);
                user.setU_authority(rs.getInt("u_authority"));
                user.setU_age(rs.getInt("u_age"));
                user.setU_sex(rs.getString("u_sex"));
                user.setU_location(rs.getString("u_location"));
                user.setU_img(rs.getString("u_img"));
                user.setU_introduction(rs.getString("u_introduction"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return user;
    }

    @Override
    public int register(User user) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "insert into user(u_name,u_pwd,u_authority,u_age,u_sex,u_location,u_img,u_introduction) values(?,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getU_name());
            ps.setString(2,user.getU_pwd());
            ps.setInt(3,user.getU_authority());
            ps.setInt(4,user.getU_age());
            ps.setString(5,user.getU_sex());
            ps.setString(6,user.getU_location());
            ps.setString(7,"d:/img/image/user/default.jpg");
            ps.setString(8,user.getU_introduction());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }

    //创建用户的购物车
    @Override
    public int createCart(int u_id) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "insert into shoppingcart(u_id,sc_num) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,u_id);
            ps.setInt(2,0);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }

    @Override
    public int pwdRex(String u_id, String old_pwd) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "select * from user where u_id=? and u_pwd=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(u_id));
            ps.setString(2,old_pwd);
            rs = ps.executeQuery();
            if(rs.next()) {
                result = rs.getInt("u_id");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }

    @Override
    public int changePwd(String new_pwd, String u_id) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "update user set u_pwd=? where u_id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,new_pwd);
            ps.setInt(2,Integer.parseInt(u_id));
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }

    @Override
    public User selectUserByName(String u_name) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "select * from user where u_name=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,u_name);
            rs = ps.executeQuery();
            if(rs.next()) {
                user = new User();
                user.setU_id(rs.getInt("u_id"));
                user.setU_name(rs.getString("u_name"));
                user.setU_authority(rs.getInt("u_authority"));
                user.setU_age(rs.getInt("u_age"));
                user.setU_sex(rs.getString("u_sex"));
                user.setU_location(rs.getString("u_location"));
                user.setU_img(rs.getString("u_img"));
                user.setU_introduction(rs.getString("u_introduction"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return user;
    }

    @Override
    public User selectUserById(int user_id) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;

        try {
            conn = ConnectionUtils.getConnection();
            String sql = "select * from user where u_id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,user_id);
            rs = ps.executeQuery();
            if(rs.next()) {
                user = new User();
                user.setU_id(rs.getInt("u_id"));
                user.setU_name(rs.getString("u_name"));
                user.setU_authority(rs.getInt("u_authority"));
                user.setU_age(rs.getInt("u_age"));
                user.setU_sex(rs.getString("u_sex"));
                user.setU_location(rs.getString("u_location"));
                user.setU_img(rs.getString("u_img"));
                user.setU_introduction(rs.getString("u_introduction"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return user;
    }

    //更新用户图片
    @Override
    public int updateUserImg(User user2) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "update user set u_img=? where u_id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,user2.getU_img());
            ps.setInt(2,user2.getU_id());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }

    @Override
    public int updateUserInformation(User user, int u_id) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "update user set u_name=? ,u_age=? ,u_sex=? ,u_location=? ,u_introduction=? where u_id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getU_name());
            ps.setInt(2,user.getU_age());
            ps.setString(3,user.getU_sex());
            ps.setString(4,user.getU_location());
            ps.setString(5,user.getU_introduction());
            ps.setInt(6,u_id);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }
}
